/* Copyright (c) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package openbang.account.service.impl;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import openbang.account.service.util.SessionHelper;
import openbang.account.service.vo.BusinessEnum;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

/**
 * 上传图片
 * 原理：
 * 1. GAE提供 BlobstoreService来上传存储二进制文件，只暴露一个 blogKey给外部使用.
 *     通过 blogKey 可以让 BlobstoreService把图片输出到 resp(浏览器)
 *      已经上传到GAE服务器的图片 在 浏览器显示，是通过我们自己编写 Servlet 输出 image/jpeg , ImagesService 取出图片进行转换 输出 byte[]值。
 * 2. GAE提供ImagesService来调整图像大小，旋转、翻转和裁剪图像；可以将多个图像合并为单个图像；可以在多种格式之间转换图像数据。它还能够使用预先定义的算法提高照片质量。
 *     通过blogKey 可以让ImagesService拿到 图片的二进制内容.
 * 问题： 使用 ImagesService来调整图像大小后，如何保存? BlobstoreService等没有提供 save 的方法。
 *       解决：转换后，让用户保存到本地，然后再重新上传？
 * 
 * 流程：
 * 1.在任意的请求页面单击 <upload>进入   本Servlet /upload.do?businessType=xxx
 * 2./upload.do 初始化  基于GAE的上传url值，跳到  upload.jsp 页面，让用户选择图片，填写标题等内容。
 * 3.在upload.jsp单击上传，先提交到 GAE 专有服务处理（BlobstoreService 存储图片），然后跳转到  /uploadPost.do 
 * 4.在uploadPost.do(我们编写的) 存储我们自己的数据库信息（除了图片）, 然后以“resp.sendRedirect”的方式跳转到 displayPhoto.do 进行显示
 * 5.在 displayPhoto.jsp中(GWT实现)，用户可以设置缩小、截取, 单击<保存>提交给   MediaSerivce.updatePhtot()(我们编写的) 
 * 6.显示此次业务处理成功
 * 
 * 
 * 
 * @author aibo zeng
 *
 */

@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(UploadServlet.class.getName());

	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	 		throws IOException, ServletException {
		if(!SessionHelper.getUserLoginInfo(req).isLogined()){
		    log.warning("有黑客? 没有登录时访问 url="+req.getRequestURL().toString());
			SessionHelper.forwardToErrorPage(BusinessEnum.UPLOAD_FACEPHOTO.getDesc(),"没有登录","请重新登录", req, resp); 
			return;
		}		
		//创建提交给 GAE 的 上传处理的URL。参数：/uploadPost.do  是GAE处理后转交给我们继续处理的url
		String uploadURL = blobstoreService.createUploadUrl("/uploadPost.do");

		req.setAttribute("uploadURL", uploadURL);
		req.setAttribute("businessType",BusinessEnum.UPLOAD_FACEPHOTO.getCode());
		RequestDispatcher dispatcher = req
				.getRequestDispatcher("WEB-INF/jsp/upload.jsp");
		dispatcher.forward(req, resp);
		log.warning("return uploadURL="+uploadURL);
	}
}
